//
//  Data.m
//  dingding
//
//  Created by dingding on 15/12/2.
//  Copyright © 2015年 dingding. All rights reserved.
//

#import "StringUtil.h"
#import <CommonCrypto/CommonDigest.h>
#import <UIKit/UIKit.h>

@implementation StringUtil

+ (NSString *) md5:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (unsigned int)strlen(cStr), result );
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}


+(NSString*)encodeString:(NSString*)unencodedString{
    //进行url编码
   NSString*hString = [unencodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
//    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
//    NSString *encodedUrl = [unencodedString stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
    
    return hString;
}

//URLDEcode
+(NSString *)decodeString:(NSString*)encodedString

{
    //NSString *decodedString = [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
    
//    NSString *decodedString  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
//                                                                                                                     (__bridge CFStringRef)encodedString,
//                                                                                                                     CFSTR(""),
//                                                                                                                     CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    return [encodedString stringByRemovingPercentEncoding];;
}

+(NSString*)floatStr:(float) value
{
    NSMutableString* str=[NSMutableString stringWithFormat:@"%.02f",value];
    if([str rangeOfString:@"."].location!=NSNotFound){
        NSRange range = [str rangeOfString:@"0+?$" options:NSRegularExpressionSearch];
         if(range.length>0){
            [str replaceCharactersInRange:range withString:@""];
         }
        range = [str rangeOfString:@"[.]$" options:NSRegularExpressionSearch];
        if(range.length>0){
            [str replaceCharactersInRange:range withString:@""];
        }
    }
    return str;
}

+(NSString*)imgUrl:(NSString *)url
{
  return url;
}

+(NSString*)timeStrMin:(long long)time
{
    if(!time)return nil;
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd  HH:mm"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:(long long int)time];
    NSString *destDateString = [formatter stringFromDate:date];
    return destDateString;
}

+(NSString*)timeStr:(NSString*)time
{
    if(!time)return nil;
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd  HH:mm"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:(long long int)[time longLongValue]];
    NSString *destDateString = [formatter stringFromDate:date];
    return destDateString;
}

+(NSString*)dateStr:(NSDate*)date
{
    if(!date)return nil;
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *destDateString = [formatter stringFromDate:date];
    return destDateString;
}


+(NSString*)shortNum:(int)num
{
    if(num>=10000){
        float tmp=num/10000.0f;
        return [NSString stringWithFormat:@"%.01fw",tmp];
    }
    return [NSString stringWithFormat:@"%d",num];
}

+(NSString *) compareCurrentTime:(NSString *)str
{
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    NSDate *currentDate = [NSDate date];
    
    //得到两个时间差
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];
    long temp = 0;
    NSString *result;
    if (timeInterval/60 < 1)
    {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    else if((temp = temp/24) <3){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    if(result){
       return  result;
    }
    return str;
}


+(NSString*)intStr:(int)num
{
    return [NSString stringWithFormat:@"%d",num];
}

+(NSString*)aboutStr:(NSString*)about
{
     return [NSString stringWithFormat:@"       %@",about];
}

+(NSArray*)splitStr:(NSString*)str sep:(NSString*)sep
{
    if(!str)return nil;
    if([str rangeOfString:sep].length==0){//没找到
        NSArray* array=[NSArray arrayWithObject:str];
        return array;
    }
    return [str componentsSeparatedByString:sep];
}

+(long long)curTime
{
   return (long)[[NSDate  date] timeIntervalSince1970];
}

+(long long)curTimeMis
{
    return [[NSDate date] timeIntervalSince1970]*1000;
}

+(BOOL)isNumber:(NSString *)myString
{
    NSString * regex        = @"(/^[0-9]*$/)";
    NSPredicate * pred      = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch            = [pred evaluateWithObject:myString];
    if (isMatch) {
        return YES;
    }else{
        return NO;
    }
}

+(void)copyTxt:(NSString*)txt
{
    if(txt&&[txt isKindOfClass:[NSString class]]){
        UIPasteboard*pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string=txt;
    }
}

@end
